import defaultIcon from '@/assets/image/emoji/default-emoji.png'
import gifIcon from '@/assets/image/emoji/gif-emoji.png'
import cartoonIcon from '@/assets/image/emoji/cartoon-emoji.png'

const defaultEmoji = require.context('../../assets/image/emoji-data/default', false, /\.png$/)
const gifEmoji = require.context('../../assets/image/emoji-data/gif', false, /\.gif$/)
const cartoonEmoji = require.context('../../assets/image/emoji-data/cartoon', false, /\.png$/)

const filterData = (data, type, suffix) => {
  const arr = []
  const reg = new RegExp(`([0-9]+)_(.+)\.${suffix}`)
  data.keys().forEach((i) => {
    const match = i.match(reg)
    const index = (match && match[1]) || -1
    if (index !== -1) {
      const obj = {
        url: data(i),
        type,
        name: (match && match[2]) || '',
      }
      arr[index] = obj
      // if (index > arr.length) {
      //   arr.push(obj)
      // } else {
      //   arr.splice(index - 1, 0, obj)
      // }
    }
  })
  return arr.filter(i => i)
}

export default {
  defaultEmoji: {
    name: '默认表情',
    key: 'defaultEmoji',
    icon: defaultIcon,
    data: filterData(defaultEmoji, 'default', 'png'),
  },
  gifEmoji: {
    name: '动态表情',
    key: 'gifEmoji',
    icon: gifIcon,
    data: filterData(gifEmoji, 'gif' ,'gif'),
  },
  cartoonEmoji: {
    name: '卡通表情',
    key: 'cartoonEmoji',
    icon: cartoonIcon,
    data: filterData(cartoonEmoji, 'cartoon' ,'png'),
  },
}
